GBML manual / Constants

Constants

A constant is a fixed name for a value that never changes, and GBML gives you named constants for buttons, text alignment, path endings, tile types, colors and door modes so you never type a bare number.

What a constant is

A variable is a named box whose contents you change while the game runs: hp = hp - 1;. A constant is a name for a value that is decided once and never changes. BTN_A always means the A button. COLOR_RED always means red. You read a constant. You never assign to one.

GBML writes its own constants in capital letters, with a short prefix that says which job they are for. BTN_ is for buttons, COLOR_ is for colors. That makes them easy to spot in code, and easy to tell apart from your own variables.

Why a name instead of a number

Suppose the engine numbered its buttons and you had to remember that the A button is 0 and START is 4. You would write btn_held(4) and read it back a week later with no idea what it meant. btn_held(BTN_START) says it on the page.

There is a second reason, and it matters more. A misspelled name is an error. A wrong number is not. Type BTN_STRAT and the compiler stops and tells you. Type 5 where you meant 4 and the game builds and does the wrong thing, with nothing to point at. A named value moves the mistake from "silent" to "caught before you ever run the game".

A third reason: the engine, not you, decides what the underlying value is. If it changes how it represents a button, every constant keeps working, and code that used raw numbers would not.

The constants GBML gives you

There are 31, in six families. Each family is used by one group of functions, and the prefix tells you which.

FamilyConstantsUsed with
ButtonsBTN_A, BTN_B, BTN_L, BTN_R, BTN_START, BTN_SELECT, BTN_UP, BTN_DOWN, BTN_LEFT, BTN_RIGHT, and for a Sega Genesis pad BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_MODEbtn_held, btn_pressed, btn_released - the ten buttons a GBA has; the last five exist only on a console whose pad has them, and the check refuses a button the console lacks
Text alignmentALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHTtext_align - which part of the text sits on the x you draw at
Path endingsPATH_STOP, PATH_LOOP, PATH_PING_PONG, PATH_CONTINUEpath_start and path_end_do - what an instance does on reaching the end of a route
Tile typesTILE_PASS, TILE_SOLID, TILE_LOW_COVERtile_class, tile_set_class - open ground, a wall, or cover that blocks bodies but not projectiles
ColorsCOLOR_WHITE, COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_YELLOW, COLOR_CYAN, COLOR_MAGENTAdraw_rect_fill - the eight colors a filled rectangle can be
Door modesDOOR_TWO_WAY, DOOR_ENTER_ONLY, DOOR_EXIT_ONLYdoor_set_mode, door_mode - whether a door sends you, receives you, or both
// Draw GUI event
var hp = 3;
if (btn_pressed(BTN_A)) { hp += 1; }
text_align(ALIGN_CENTER);
draw_rect_fill(14, 145, 20, 10, COLOR_RED);

What a constant is underneath

You do not need this to use them, but it explains what you can and cannot do with one.

Limits worth knowing

Making your own

GBML has the same idea for your own numbers. #macro NAME value defines a name that is replaced by its value wherever it appears, and enum Name { A, B, C } makes a small set of named whole numbers. Put a #macro in a script and every object can use it. Put one in a single event file and only that file can. The Digger template keeps its CELL, the size of a tile, in one script this way. Expressions and statements covers both.

See also: Variables, types and scopeExpressions and statementsbtn_heldtext_alignpath_startdraw_rect_filldoor_set_mode